#!/usr/bin/env python
#
#       Bounce.py
#       
#       Copyright 2009 Pete Lord <mr.petelord@gmail.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import random

class Bouncer():
    def __init__(self, amount, max, min, speed):
        # how much is the ship offset for whatever reason?
        self.mod = random.randint(min, max)
        self.amount = amount
        
        self.pushing = 0 # are we pushing?
        
        self.max = max
        self.min = min
        self.speed = self.current_time = speed
            
    def poll(self):
        # update the status of the bouncer.
        if self.pushing == 0: # we aren't pushing from centre.
            self.current_time -= 1                              # increment
            if self.current_time == 0:                          # ready?
                self.mod += self.amount                         # mod.
                if self.mod > self.max or self.mod < self.min:  # overboundaries?
                    self.amount = -self.amount                  # other way!
                self.current_time = self.speed                  # and reset.
        else: # we are pushing from the centre
            if self.pushing > -1 and self.pushing < 0 or self.pushing < 1 and self.pushing > 0:
                self.pushing = 0
            elif self.pushing < 0:
                self.pushing += 2
            else:
                self.pushing -= 2
            
    def push(self, amount):
        self.pushing = amount
        
    def value(self):
        return self.mod + self.pushing
        
class BounceXY():
    """ holds two bouncers, and applys pushes isometrically. woo"""
    def __init__(self, x, y):
        self.x = Bouncer(x[0], x[1], x[2], x[3])
        self.y = Bouncer(y[0], y[1], y[2], y[3])
        
    def poll(self):
        self.x.poll()
        self.y.poll()
        
    def push(self, amount, direction):
        x_mod = 0
        y_mod = 0
        if direction == 0:
            x_mod =  0.8
            y_mod = -0.8
        elif direction == 1:
            x_mod =  1.0
            y_mod =  0.0
        elif direction == 2:
            x_mod =  0.8
            y_mod =  0.8
        elif direction == 3:
            x_mod =  0.0
            y_mod =  1.0           
        elif direction == 4:
            x_mod = -0.8
            y_mod =  0.8
        elif direction == 5:
            x_mod = -1.0
            y_mod =  0.0
        elif direction == 6:
            x_mod = -0.8
            y_mod = -0.8  
        elif direction == 7:
            x_mod =  0.0
            y_mod = -1.0
            
        self.x.push(amount * x_mod)
        self.y.push(amount * y_mod)
